package com.example.travel.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.travel.common.ValidationEnum;
import com.example.travel.domain.entity.Validation;
import com.example.travel.mapper.ValidationMapper;
import com.example.travel.service.ValidationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author : LinQingLiang
 * @file : ValidationServiceImpl.java
 * @Since : 2023/2/27 9:57
 */
@Service
public class ValidationServiceImpl extends ServiceImpl<ValidationMapper, Validation> implements ValidationService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveCode(String email, String code, Integer type, DateTime expireTime) {
        Validation validation = new Validation();
        validation.setEmail(email);
        validation.setCode(code);
        validation.setTime(expireTime);
        validation.setType(type);

        //删除同类型的验证
        QueryWrapper<Validation> wrapper = new QueryWrapper<>();
        wrapper.eq("email",email).
                eq("type", type);
        remove(wrapper);

        save(validation);
    }
}
